﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseFund.Common;
using DevExpress.Web.Mvc;
using System.Data;
using Newtonsoft.Json;
using System.Text;
using System.IO;

namespace EnterpriseFund.Controllers
{
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class TaxPayerManagerController : AdminBaseController
    {
        //
        // GET: /TaxPayerManager/

        [AuthorityActionFilter("m_TaxPayerManager", Order = 1)]
        [LogPageActionFilter("企业信息管理", Order = 2)]
        public ActionResult Index(string NSRMC, string NSRBM, string HY_DM, string FEnterprisePropNo, string FFinancePropNo, string IsNoFull, string SKHJ1, string SKHJ2)
        {

            string strwhere = "";

            BLL.EnterpriseProp EnterprisePropBLL = new BLL.EnterpriseProp();

            ViewData["EnterprisePropList"] = EnterprisePropBLL.GetModelList("");

            BLL.FinanceProp FinancePropBLL = new BLL.FinanceProp();

            ViewData["FinancePropList"] = FinancePropBLL.GetModelList("");

            BLL.DM_HY DM_HYBLL = new BLL.DM_HY();

            ViewData["DMHYList"] = DM_HYBLL.GetModelList("");


            ViewData["strwhere"] = System.Web.HttpUtility.UrlEncode(strwhere, System.Text.Encoding.GetEncoding("utf-8"));
            BLL.DJ_NSRXX DJ_NSRXXBLL = new BLL.DJ_NSRXX();
            DataTable DJ_NSRXXList = new DataTable();
            // DJ_NSRXXList = GetDJ_NSRXXListList(strwhere, pageIndex);
            return View(DJ_NSRXXList);
        }

        public ActionResult TaxPayerManagerListPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            string callbackArg = DevExpressHelper.CallbackArgument;

            string NSRMC = Request.Params["NSRMC"];
            string NSRBM = Request.Params["NSRBM"];
            string HY_DM = Request.Params["HY_DM"];
            string SKHJ1 = Request.Params["SKHJ1"];
            string SKHJ2 = Request.Params["SKHJ2"];
            string FEnterprisePropNo = Request.Params["FEnterprisePropNo"];
            string FFinancePropNo = Request.Params["FFinancePropNo"];
            string IsNoFull = Request.Params["IsNoFull"];
            string IsMrTax = Request.Params["IsMrTax"];
            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }
            string strwhere = "1=1";

            if (NSRMC != "")
            {
                strwhere += " and NSRMC like  '%" + NSRMC + "%'";
            }
            if (NSRBM != "")
            {
                strwhere += " and NSRBM like '%" + NSRBM + "%'";
            }
            if (HY_DM != "")
            {
                strwhere += " and HY_DM='" + HY_DM + "'";
            }
            if (FEnterprisePropNo != "")
            {
                strwhere += " and FEnterprisePropNo='" + FEnterprisePropNo + "'";
            }
            if (FFinancePropNo != "")
            {
                strwhere += " and FFinancePropNo='" + FFinancePropNo + "'";
            }
            if (IsNoFull != "")
            {
                if (IsNoFull == "Y")
                {
                    strwhere += " and  (FFinancePropNo is null or FEnterprisePropNo is null)";
                }
            }


            

            string strwhere2 = "";
            if (IsMrTax == "Y")
            {
                strwhere2 = "((FEnterprisePropNo='01' and se>500000) or (FEnterprisePropNo<>'01' and se >400000) or (FEnterprisePropNo is null and se >400000))";
            }
            else
            {
                if (SKHJ2 != null && SKHJ2 != "" && SKHJ1 != null && SKHJ1 != "")
                {
                    decimal temp = 0;
                    if (decimal.TryParse(SKHJ1, out temp) && decimal.TryParse(SKHJ2, out temp))
                    {
                        strwhere2 += "  isnull(se,0) >=" + decimal.Parse(SKHJ1) * 10000 + " and isnull(se,0)  <=" + decimal.Parse(SKHJ2) * 10000;
                    }
                }
                else if (SKHJ2 != null && SKHJ2 != "")
                {
                    decimal temp = 0;
                    if (decimal.TryParse(SKHJ2, out temp))
                    {
                        strwhere2 += "  isnull(se,0)  <=" + decimal.Parse(SKHJ2) * 10000;
                    }
                }
                else if (SKHJ1 != null && SKHJ1 != "")
                {
                    decimal temp = 0;
                    if (decimal.TryParse(SKHJ1, out temp))
                    {
                        strwhere2 += "  isnull(se,0)  >=" + decimal.Parse(SKHJ1) * 10000;
                    }
                }
            }
            BLL.DJ_NSRXX DJ_NSRXXBLL = new BLL.DJ_NSRXX();
            DataTable DJ_NSRXXList = new DataTable();
            DJ_NSRXXList = GetDJ_NSRXXListList(strwhere, pageIndex, strwhere2);
            return PartialView("TaxPayerManagerListPartial", DJ_NSRXXList);
        }

        public ActionResult PropALLEdit(string noList, string pageNoList, string strwhere)
        {

            ViewData["noList"] = noList;
            ViewData["pageNoList"] = pageNoList;
            ViewData["strwhere"] = strwhere;

            BLL.EnterpriseProp EnterprisePropBLL = new BLL.EnterpriseProp();

            ViewData["EnterprisePropList"] = EnterprisePropBLL.GetModelList("");

            BLL.FinanceProp FinancePropBLL = new BLL.FinanceProp();

            ViewData["FinancePropList"] = FinancePropBLL.GetModelList("");

            return View();
        }

        public string ExistsProp(string noList, string type)
        {
            try
            {
                string selectno = "";
                BLL.DJ_NSRXX DJ_NSRXXBLL = new BLL.DJ_NSRXX();
                if (type != "3")
                {
                    string[] nolists = noList.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string no in nolists)
                    {
                        if (selectno != "")
                        {
                            selectno += "," + no;
                        }
                        else
                        {
                            selectno += no;
                        }
                    }
                }
                else
                {
                    selectno = noList;
                }
                string year = OperateSession.get(GlobalConstant.PARAM_SYSYEAR);
                BLL.Common CommonBLL = new BLL.Common();
                string str = CommonBLL.GetGetRejectTaxSqlByYear(year);
                string strwhere1 = "";
                if (str != "" && str != "( NULL)     ")
                {
                    strwhere1 = @"   NOT EXISTS (SELECT  ZSXH
         FROM fund.V_SB_ZSXX s WHERE (" + str + @")  AND
                                   r.FDataSource = s.FDataSource AND
                                   r.ZSXH = s.ZSXH ) and DATEDIFF(DAY,RKRQ,'" + year + "-01-01')<=0 AND DATEDIFF(DAY,RKRQ,'" + year + "-12-31')>=0 ";
                }
                else
                {
                    strwhere1 = @" DATEDIFF(DAY,RKRQ,'" + year + "-01-01')<=0 AND DATEDIFF(DAY,RKRQ,'" + year + "-12-31')>=0 ";
                }
                if (DJ_NSRXXBLL.Exists(decimal.Parse(OperateSession.get(GlobalConstant.PARAM_SYSYEAR)), selectno, type, strwhere1))
                {
                    return "1";
                }
                else
                {
                    return "2";
                }
            }
            catch
            {
                return "3";
            }
        }
        public ActionResult ImportExcel()
        {
            return View("");
        }
        public ActionResult ImportData()
        {
            BLL.EnterpriseFinanceProp efbll = new BLL.EnterpriseFinanceProp();
            BLL.EnterpriseProp enpbll = new BLL.EnterpriseProp();
            BLL.FinanceProp fanbll = new BLL.FinanceProp();
            BLL.DJ_NSRXX_Extension1 DJ_NSRXX_Extension1BLL = new BLL.DJ_NSRXX_Extension1();
            BLL.DJ_NSRXX nsrbll = new BLL.DJ_NSRXX();
            string type = Fetch.Post("type");
            var ErrMsg = "";
            try
            {
                //上传
                var uploadFiles = UploadControlExtension.GetUploadedFiles("upload");
                if (null == uploadFiles || uploadFiles.Length == 0)
                {
                    ErrMsg = "请选择Excel文件！";
                    //return ErrMsg;
                }
                var upload = uploadFiles[0];
                var clintFileName = upload.FileName;
                var extName = clintFileName.Substring(clintFileName.LastIndexOf('.') + 1);
                var svrFileName = string.Format("{0}.{1}", DateTime.Now.ToString("yyyyMMddHHmm"), extName);
                // var uploadoffiepath = string.Format("~/Publish/{0}/QTBZ/{1}{2}/", GlobalConstant.ENTITYNO, DateTime.Now.Year, DateTime.Now.Month);
                var uploadoffiepath = Server.MapPath(Url.Content("~") + string.Format("/Publish/{0}/QTBZ/{1}{2}/", GlobalConstant.ENTITYNO, DateTime.Now.Year, DateTime.Now.Month));
                if (!Directory.Exists(uploadoffiepath))//如果不存在就创建
                {
                    Directory.CreateDirectory(uploadoffiepath);
                }
                upload.SaveAs(uploadoffiepath + svrFileName);

                //读取
                var dt = new BLL.SB_ZSXX().ReadExcelTitle(uploadoffiepath + svrFileName).Tables[0];

                //验证
                var cols = new List<string> { "企业名称", "纳税编码", "企业性质", "资金项目" };
                foreach (var col in cols)
                {
                    if (!dt.Columns.Contains(col))
                    {
                        ErrMsg = "Excel文件格式错误！";
                        // return ErrMsg;
                    }
                }

                List<Model.DJ_NSRXX_Extension1> model1List = new List<Model.DJ_NSRXX_Extension1>();
                List<Model.DJ_NSRXX_Extension1> updatelist = new List<Model.DJ_NSRXX_Extension1>();
                for (var i = 0; i < dt.Rows.Count; i++)
                {
                    var r = dt.Rows[i];
                    var excelRowIndex = i + 2;



                    if (r.IsNull("企业名称") || string.IsNullOrEmpty(r["企业名称"].ToString()))
                    {
                        ErrMsg += "Excel文件第" + excelRowIndex + "行，企业名称不能为空！";

                        
                        // return ErrMsg;
                    }


                    if (r.IsNull("纳税编码") || string.IsNullOrEmpty(r["纳税编码"].ToString()))
                    {
                        ErrMsg += "Excel文件第" + excelRowIndex + "行，纳税编码数据有误！";
                       
                        //  return ErrMsg;
                    }
                    if (r.IsNull("企业性质") || string.IsNullOrEmpty(r["企业性质"].ToString()))
                    {

                        ErrMsg += "Excel文件第" + excelRowIndex + "行，企业性质数据有误！";
                       
                        //   return ErrMsg;

                    }
                    if (r.IsNull("资金项目") || string.IsNullOrEmpty(r["资金项目"].ToString()))
                    {

                        ErrMsg += "Excel文件第" + excelRowIndex + "行，资金项目数据有误！";
                      
                        //   return ErrMsg;

                    }
                    Model.DJ_NSRXX n = new Model.DJ_NSRXX();
                    string NSRDZDAH = null;
                    try
                    {

                        n = nsrbll.GetModelByNSRBM(r["纳税编码"].ToString(), OperateSession.get(GlobalConstant.PARAM_SYSYEAR));

                    }
                    catch (System.Exception ex)
                    {
                        ErrMsg += "Excel文件第" + excelRowIndex + "行" + r["企业名称"].ToString() + "纳税人电子档案号不存在" + ex.Message;
                      continue;
                        //break;
                    }
                    if (n==null)
                    {
                        ErrMsg += "Excel文件第" + excelRowIndex + "行" + r["企业名称"].ToString() + "纳税人不存在" ;
                        continue;
                        // break;
                    }
                    else
                    {
                        if (n.NSRMC.Trim() == r["企业名称"].ToString().Trim())
                        {
                            NSRDZDAH = n.NSRDZDAH.ToString();
                        } 
                        else
                        {
                            ErrMsg += "Excel文件第" + excelRowIndex + "行" + r["企业名称"].ToString() + "企业名称有误";
                            continue;
                            //break;
                        }
                      
                    }
                  
                    string rFEnterprisePropNo = string.Empty;
                    try
                    {
                        rFEnterprisePropNo = enpbll.GetFEnterprisePropNoByFEnterprisePropName(r["企业性质"].ToString());
                    }
                    catch (System.Exception ex)
                    {
                        ErrMsg += "Excel文件第" + excelRowIndex + "行" + r["企业名称"].ToString() + "企业性质不存在" + ex.Message;
                       // break;
                    }
                    string rFFinancePropNo = string.Empty;
                    try
                    {
                        rFFinancePropNo = fanbll.GetFFinancePropNoByFFinancePropShortName(r["资金项目"].ToString());
                    }
                    catch (System.Exception ex)
                    {
                        ErrMsg += "Excel文件第" + excelRowIndex + "行" + r["企业名称"].ToString() + "资金项目不存在" + ex.Message;
                        //break;
                    }

                    if (!string.IsNullOrEmpty(rFEnterprisePropNo) & !string.IsNullOrEmpty(rFFinancePropNo))
                    {
                        if (efbll.GetModel(rFFinancePropNo, decimal.Parse(OperateSession.get(GlobalConstant.PARAM_SYSYEAR)), rFEnterprisePropNo) != null)
                        {

                            if (DJ_NSRXX_Extension1BLL.Exists(decimal.Parse(NSRDZDAH), decimal.Parse(OperateSession.get(GlobalConstant.PARAM_SYSYEAR)), GlobalConstant.ENTITYNO) & type == "C")
                            {
                                Model.DJ_NSRXX_Extension1 modelz = new Model.DJ_NSRXX_Extension1();
                                modelz.FYear = decimal.Parse(OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
                                modelz.FEntityNo = GlobalConstant.ENTITYNO;
                                modelz.FEnterprisePropNo = rFEnterprisePropNo;
                                modelz.FFinancePropNo = rFFinancePropNo;
                                modelz.FInsDate = DateTime.Now;
                                modelz.FInsDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                                modelz.FInsEmpNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                                modelz.FInsIP = GenerateUtil.GetClientIPv4Address();
                                modelz.NSRDZDAH = decimal.Parse(NSRDZDAH);

                                updatelist.Add(modelz);
                            }
                            else
                            {
                                Model.DJ_NSRXX_Extension1 modelz = new Model.DJ_NSRXX_Extension1();
                                modelz.FYear = decimal.Parse(OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
                                modelz.FEntityNo = GlobalConstant.ENTITYNO;
                                modelz.FEnterprisePropNo = rFEnterprisePropNo;
                                modelz.FFinancePropNo = rFFinancePropNo;
                                modelz.FInsDate = DateTime.Now;
                                modelz.FInsDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                                modelz.FInsEmpNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                                modelz.FInsIP = GenerateUtil.GetClientIPv4Address();
                                modelz.NSRDZDAH = decimal.Parse(NSRDZDAH);
                                model1List.Add(modelz);
                            }
                        }
                        else
                        {
                            ErrMsg += "Excel文件第" + excelRowIndex + "行" + r["企业名称"].ToString() + "企业性质与资金项目关联不存在请核对";
                           // break;
                        }
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(rFEnterprisePropNo))
                        {
                            ErrMsg += "Excel文件第" + excelRowIndex + "行" + r["企业名称"].ToString() + "企业性质不存在";
                          //  break;
                        }
                        if (string.IsNullOrEmpty(rFFinancePropNo))
                        {
                            ErrMsg += "Excel文件第" + excelRowIndex + "行" + r["企业名称"].ToString() + "资金项目不存在";
                           // break;
                        }
                    }



                }
                if (ErrMsg == "")
                {

                    string outstr = DJ_NSRXX_Extension1BLL.AddALL(model1List, updatelist);
                    if (outstr == "")
                    {
                        ViewData["jsAlert"] = "导入成功";
                    }
                    else
                    {
                        ViewData["jsAlert"] = outstr;
                    }
                }
                else
                {

                    ViewData["jsAlert"] = ErrMsg;
                }
                //   upLevelAllowanceBLL.Update2(upLevelAllowanceEntryList, modela);



            }
            catch (System.Exception ex)
            {
                ViewData["jsAlert"] = "error" + ex.Message.ToString() + ErrMsg;
            }
            return View("ImportExcel");
        }
        public ActionResult PropALLSave()
        {
            BLL.DJ_NSRXX DJ_NSRXXBLL = new BLL.DJ_NSRXX();
            string type = Fetch.Post("type");
            string noList = Fetch.Post("hidnoList");
            string pageNoList = Fetch.Post("hidpageNoList");
            string strwhere = Fetch.Post("hidstrwhere");
            string fEnterprisePropNo = DevExpress.Web.Mvc.ComboBoxExtension.GetValue<string>("FEnterprisePropNo"); //Fetch.Post("FEnterprisePropNo");
            string fFinancePropNo = DevExpress.Web.Mvc.ComboBoxExtension.GetValue<string>("FFinancePropNo"); //Fetch.Post("FFinancePropNo");

            try
            {
                StringBuilder selectNo = new StringBuilder();
                //string selectNo = "";
                List<Model.DJ_NSRXX_Extension1> DJ_NSRXX_Extension1List = new List<Model.DJ_NSRXX_Extension1>();
                switch (type)
                {
                    case "0":
                        string[] nolists = noList.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (string no in nolists)
                        {
                            if (selectNo.ToString() != "")
                            {
                                selectNo.Append("," + no);
                            }
                            else
                            {
                                selectNo.Append(no);
                            }
                            Model.DJ_NSRXX_Extension1 model = new Model.DJ_NSRXX_Extension1();
                            model.FYear = decimal.Parse(OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
                            model.FEntityNo = GlobalConstant.ENTITYNO;
                            model.FEnterprisePropNo = fEnterprisePropNo;
                            model.FFinancePropNo = fFinancePropNo;
                            model.FInsDate = DateTime.Now;
                            model.NSRDZDAH = decimal.Parse(no);
                            model.FInsDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                            model.FInsEmpNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                            model.FInsIP = GenerateUtil.GetClientIPv4Address();
                            DJ_NSRXX_Extension1List.Add(model);

                        }
                        DJ_NSRXXBLL.AddALL(DJ_NSRXX_Extension1List, selectNo.ToString());
                        break;
                    case "1":
                        string[] pagenolists = pageNoList.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (string no in pagenolists)
                        {
                            if (selectNo.ToString() != "")
                            {
                                selectNo.Append("," + no);
                            }
                            else
                            {
                                selectNo.Append(no);
                            }
                            Model.DJ_NSRXX_Extension1 model = new Model.DJ_NSRXX_Extension1();
                            model.FYear = decimal.Parse(OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
                            model.FEntityNo = GlobalConstant.ENTITYNO;
                            model.FEnterprisePropNo = fEnterprisePropNo;
                            model.FFinancePropNo = fFinancePropNo;
                            model.FInsDate = DateTime.Now;
                            model.NSRDZDAH = decimal.Parse(no);
                            model.FInsDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                            model.FInsEmpNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                            model.FInsIP = GenerateUtil.GetClientIPv4Address();
                            DJ_NSRXX_Extension1List.Add(model);
                        }
                        DJ_NSRXXBLL.AddALL(DJ_NSRXX_Extension1List, selectNo.ToString());
                        break;
                    case "2":
                        //List<string> noStrList = DJ_NSRXXBLL.GetDzdah(OperateSession.get(GlobalConstant.PARAM_SYSYEAR), strwhere);
                        //foreach (string no in noStrList)
                        //{
                        //    if (selectNo.ToString() != "")
                        //    {
                        //        selectNo.Append("," + no);
                        //    }
                        //    else
                        //    {
                        //        selectNo.Append(no);
                        //    }
                        //    Model.DJ_NSRXX_Extension1 model = new Model.DJ_NSRXX_Extension1();
                        //    model.FYear = decimal.Parse(OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
                        //    model.FEntityNo = GlobalConstant.ENTITYNO;
                        //    model.FEnterprisePropNo = fEnterprisePropNo;
                        //    model.FFinancePropNo = fFinancePropNo;
                        //    model.FInsDate = DateTime.Now;
                        //    model.NSRDZDAH = decimal.Parse(no);
                        //    model.FInsDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                        //    model.FInsEmpNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                        //    model.FInsIP = GenerateUtil.GetClientIPv4Address();
                        //    DJ_NSRXX_Extension1List.Add(model);
                        //}

                        Model.DJ_NSRXX_Extension1 modelz = new Model.DJ_NSRXX_Extension1();
                        modelz.FYear = decimal.Parse(OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
                        modelz.FEntityNo = GlobalConstant.ENTITYNO;
                        modelz.FEnterprisePropNo = fEnterprisePropNo;
                        modelz.FFinancePropNo = fFinancePropNo;
                        modelz.FInsDate = DateTime.Now;
                        modelz.FInsDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                        modelz.FInsEmpNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                        modelz.FInsIP = GenerateUtil.GetClientIPv4Address();

                        string year = OperateSession.get(GlobalConstant.PARAM_SYSYEAR);
                        BLL.Common CommonBLL = new BLL.Common();
                        string str = CommonBLL.GetGetRejectTaxSqlByYear(year);
                        string strwhere1 = "";
                        if (str != "" && str != "( NULL)     ")
                        {
                            strwhere1 = @"   NOT EXISTS (SELECT  ZSXH
         FROM fund.V_SB_ZSXX s WHERE (" + str + @")  AND
                                   r.FDataSource = s.FDataSource AND
                                   r.ZSXH = s.ZSXH ) and DATEDIFF(DAY,RKRQ,'" + year + "-01-01')<=0 AND DATEDIFF(DAY,RKRQ,'" + year + "-12-31')>=0 ";
                        }
                        else
                        {
                            strwhere1 = @" DATEDIFF(DAY,RKRQ,'" + year + "-01-01')<=0 AND DATEDIFF(DAY,RKRQ,'" + year + "-12-31')>=0 ";
                        }

                        DJ_NSRXXBLL.AddALLPre(strwhere, OperateSession.get(GlobalConstant.PARAM_SYSYEAR), modelz, strwhere1);
                        break;
                }

                ViewData["jsAlert"] = "操作成功";
            }
            catch
            {
                ViewData["jsAlert"] = "操作异常";
            }
            ViewData["noList"] = noList;
            ViewData["pageNoList"] = pageNoList;
            ViewData["strwhere"] = strwhere;

            BLL.EnterpriseProp EnterprisePropBLL = new BLL.EnterpriseProp();

            ViewData["EnterprisePropList"] = EnterprisePropBLL.GetModelList("");

            BLL.FinanceProp FinancePropBLL = new BLL.FinanceProp();

            ViewData["FinancePropList"] = FinancePropBLL.GetModelList("");

            return View("PropALLEdit");
        }

        public ActionResult PropEdit(string NSRDZDAH)
        {
            BLL.EnterpriseProp EnterprisePropBLL = new BLL.EnterpriseProp();

            ViewData["EnterprisePropList"] = EnterprisePropBLL.GetModelList("");

            BLL.FinanceProp FinancePropBLL = new BLL.FinanceProp();

            ViewData["FinancePropList"] = FinancePropBLL.GetModelList("");
            ViewData["NSRDZDAH"] = NSRDZDAH;
            BLL.DJ_NSRXX DJ_NSRXXBLL = new BLL.DJ_NSRXX();
            Model.DJ_NSRXX DJ_NSRXXModel = new Model.DJ_NSRXX();
            DJ_NSRXXModel = DJ_NSRXXBLL.GetModel(decimal.Parse(NSRDZDAH), OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
            return View(DJ_NSRXXModel);
        }

        public ActionResult PropSave(Model.DJ_NSRXX model)
        {
            string NSRDZDAH = Fetch.Post("hidNSRDZDAH");
            BLL.DJ_NSRXX_Extension1 DJ_NSRXX_Extension1BLL = new BLL.DJ_NSRXX_Extension1();
            BLL.DJ_NSRXX_Extension2 DJ_NSRXX_Extension2BLL = new BLL.DJ_NSRXX_Extension2();
            BLL.DJ_NSRXX DJ_NSRXXBLL = new BLL.DJ_NSRXX();
            try
            {
                Model.DJ_NSRXX_Extension1 model1 = DJ_NSRXX_Extension1BLL.GetModel(decimal.Parse(NSRDZDAH), decimal.Parse(OperateSession.get(GlobalConstant.PARAM_SYSYEAR)), GlobalConstant.ENTITYNO);
                Model.DJ_NSRXX_Extension2 model2 = DJ_NSRXX_Extension2BLL.GetModel(decimal.Parse(NSRDZDAH), GlobalConstant.ENTITYNO);
                Model.DJ_NSRXX_Extension1 DJ_NSRXX_Extension1Model = new Model.DJ_NSRXX_Extension1();
                Model.DJ_NSRXX_Extension2 DJ_NSRXX_Extension2Model = new Model.DJ_NSRXX_Extension2();
                if (model1 != null)
                {
                    DJ_NSRXX_Extension1Model.FInsDate = model1.FInsDate;
                    DJ_NSRXX_Extension1Model.FInsDeptNo = model1.FInsDeptNo;
                    DJ_NSRXX_Extension1Model.FInsEmpNo = model1.FInsEmpNo;
                    DJ_NSRXX_Extension1Model.FInsIP = model1.FInsIP;
                    DJ_NSRXX_Extension1Model.FUpdDate = DateTime.Now;
                    DJ_NSRXX_Extension1Model.FUpdDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                    DJ_NSRXX_Extension1Model.FUpdEmplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                    DJ_NSRXX_Extension1Model.FUpdIP = GenerateUtil.GetClientIPv4Address();
                }
                else
                {
                    DJ_NSRXX_Extension1Model.FInsDate = DateTime.Now;
                    DJ_NSRXX_Extension1Model.FInsDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                    DJ_NSRXX_Extension1Model.FInsEmpNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                    DJ_NSRXX_Extension1Model.FInsIP = GenerateUtil.GetClientIPv4Address();
                }

                if (model2 != null)
                {
                    DJ_NSRXX_Extension2Model.FInsDate = model2.FInsDate;
                    DJ_NSRXX_Extension2Model.FInsDeptNo = model2.FInsDeptNo;
                    DJ_NSRXX_Extension2Model.FInsEmpNo = model2.FInsEmpNo;
                    DJ_NSRXX_Extension2Model.FInsIP = model2.FInsIP;
                    DJ_NSRXX_Extension2Model.FUpdDate = DateTime.Now;
                    DJ_NSRXX_Extension2Model.FUpdDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                    DJ_NSRXX_Extension2Model.FUpdEmplNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                    DJ_NSRXX_Extension2Model.FUpdIP = GenerateUtil.GetClientIPv4Address();
                }
                else
                {
                    DJ_NSRXX_Extension2Model.FInsDate = DateTime.Now;
                    DJ_NSRXX_Extension2Model.FInsDeptNo = OperateSession.get(GlobalConstant.PARAM_DEPTNO);
                    DJ_NSRXX_Extension2Model.FInsEmpNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
                    DJ_NSRXX_Extension2Model.FInsIP = GenerateUtil.GetClientIPv4Address();
                }

                DJ_NSRXX_Extension2Model.GS_NSRMC = model.GS_NSRMC;
                DJ_NSRXX_Extension2Model.GS_NSRSBH = model.GS_NSRSBH;
                DJ_NSRXX_Extension2Model.FEntityNo = GlobalConstant.ENTITYNO;
                DJ_NSRXX_Extension1Model.FEnterprisePropNo = model.FEnterprisePropNo;
                DJ_NSRXX_Extension1Model.FFinancePropNo = model.FFinancePropNo;
                DJ_NSRXX_Extension1Model.FEntityNo = GlobalConstant.ENTITYNO;

                DJ_NSRXX_Extension1Model.NSRDZDAH = decimal.Parse(NSRDZDAH);
                DJ_NSRXX_Extension2Model.NSRDZDAH = decimal.Parse(NSRDZDAH);

                DJ_NSRXX_Extension1Model.FYear = decimal.Parse(OperateSession.get(GlobalConstant.PARAM_SYSYEAR));

                DJ_NSRXXBLL.Add(DJ_NSRXX_Extension1Model, DJ_NSRXX_Extension2Model);

                ViewData["jsAlert"] = "操作成功";
            }
            catch
            {
                ViewData["jsAlert"] = "操作异常";
            }
            BLL.EnterpriseProp EnterprisePropBLL = new BLL.EnterpriseProp();

            ViewData["EnterprisePropList"] = EnterprisePropBLL.GetModelList("");

            BLL.FinanceProp FinancePropBLL = new BLL.FinanceProp();
            ViewData["NSRDZDAH"] = NSRDZDAH;
            ViewData["FinancePropList"] = FinancePropBLL.GetModelList("");
            Model.DJ_NSRXX DJ_NSRXXModel = new Model.DJ_NSRXX();
            DJ_NSRXXModel = DJ_NSRXXBLL.GetModel(decimal.Parse(NSRDZDAH), OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
            return View("PropEdit", DJ_NSRXXModel);
        }

        /// <summary>
        /// 获取下拉框的值
        /// </summary>
        /// <param name="fEnterprisePropNo"></param>
        /// <returns></returns>
        public string GetList(string fEnterprisePropNo)
        {
            BLL.FinanceProp financePropBLL = new BLL.FinanceProp();
            List<Model.FinanceProp> financePropList = financePropBLL.GetSelectList(fEnterprisePropNo, OperateSession.get(GlobalConstant.PARAM_SYSYEAR));
            return JsonConvert.SerializeObject(financePropList);
        }

        /// <summary>
        /// 获取定义的工作时间数据
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns>查询出来的数据</returns>
        private DataTable GetDJ_NSRXXListList(string strWhere, int pageIndex,string strWhere2)
        {
            string year = OperateSession.get(GlobalConstant.PARAM_SYSYEAR);
            BLL.Common CommonBLL = new BLL.Common();
            string str = CommonBLL.GetGetRejectTaxSqlByYear(year);
            string strwhere1 = "";
            if (str != "" && str != "( NULL)     ")
            {
                strwhere1 = @"   NOT EXISTS (SELECT  ZSXH
         FROM fund.V_SB_ZSXX s WHERE (" + str + @")  AND
                                   r.FDataSource = s.FDataSource AND
                                   r.ZSXH = s.ZSXH ) and DATEDIFF(DAY,RKRQ,'" + year + "-01-01')<=0 AND DATEDIFF(DAY,RKRQ,'" + year + "-12-31')>=0 ";
            }
            else
            {
                strwhere1 = @" DATEDIFF(DAY,RKRQ,'" + year + "-01-01')<=0 AND DATEDIFF(DAY,RKRQ,'" + year + "-12-31')>=0 ";
            }

            BLL.DJ_NSRXX DJ_NSRXXBLL = new BLL.DJ_NSRXX();
            DataSet ds = DJ_NSRXXBLL.GetShowList(strwhere1, strWhere2, year, strWhere);

            DataTable dt = ds.Tables[0];
            dt.Columns.Add("NSRDZDAHstr");

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i]["NSRDZDAHstr"] = dt.Rows[i]["NSRDZDAH"].ToString();
            }

            return dt;
        }

        public string ImportDate()
        {
            try
            {
                // BLL.DSDJ_NSRXX DSDJ_NSRXXBLL = new BLL.DSDJ_NSRXX();
                // BLL.DSDJ_NSRXX_KZ DSDJ_NSRXX_KZBLL = new BLL.DSDJ_NSRXX_KZ();

                ////List<Model.DSDJ_NSRXX> DSDJ_NSRXXList = DSDJ_NSRXXBLL.GetModelList(" nsr_swjg_dm like '2330209%'");
                // //List<Model.DSDJ_NSRXX_KZ> DSDJ_NSRXX_KZList = DSDJ_NSRXX_KZBLL.GetModelList(" nsr_swjg_dm like '2330209%'");
                // List<Model.DSDJ_NSRXX> DSDJ_NSRXXList = DSDJ_NSRXXBLL.GetModelList("");
                // List<Model.DSDJ_NSRXX_KZ> DSDJ_NSRXX_KZList = DSDJ_NSRXX_KZBLL.GetModelList("");

                // BLL.DJ_YHXX DJ_YHXXBLL = new BLL.DJ_YHXX();
                // List<Model.DJ_YHXX> DJ_YHXXList = DJ_YHXXBLL.GetModelList("YXBZ='Y' AND nsr_swjg_dm like '2330209%'");

                BLL.DJ_NSRXX DJ_NSRXXBLL = new BLL.DJ_NSRXX();
                DJ_NSRXXBLL.ImportData(" nsr_swjg_dm like '2330209%' ");
                return "ok";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

    }
}
